//
//  postModel.m
//  Item A House
//
//  Created by 甄奇文 on 15/5/28.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "postModel.h"
#import "Reachability.h"
#import "Tools.h"
@implementation postModel
- (void)dealloc
{
    [_requestUrl release];
    [super dealloc];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}
#pragma mark -
#pragma 判断是否有网封装
- (BOOL)stringFromStatus:(NetworkStatus)status
{
    BOOL a = YES;
    switch (status) {
        case NotReachable:
            a = NO;
            break;
        case ReachableViaWiFi:
        case ReachableViaWWAN:
            break;
        default:
            a = NO;
            break;
    }
    return a;
}

+ (postModel *)connectWithUrl:(NSString *)url delegate:(id<postModelDelegate>)delegate parmaters:(NSDictionary *)parmaters
{
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    if (status) {
        postModel *model = [[postModel alloc] init];
        model.delegate = delegate;
        [model startConnection:url parmaters:parmaters];
        NSLog(@"有网");
        return [model autorelease];
    } else {
        NSString *str = [url stringByAppendingString:[parmaters objectForKey:@"cityid"]];
        str = [str stringByAppendingString:[parmaters objectForKey:@"hid"]];
        NSData *data = [Tools readWithName:str directory:NSLibraryDirectory];
        [delegate connectFinished:data];
        NSLog(@"无网");
        
        
        
        return nil;
    }
    
}

- (void)startConnection:(NSString *)str parmaters:(NSDictionary *)parmaters
{
    // 异步网络连接
    NSString *string = [str stringByAppendingString:[parmaters objectForKey:@"cityid"]];
    self.requestUrl = [string stringByAppendingString:[parmaters objectForKey:@"hid"]];
    
    
    
    NSString *bodyStr = @"--0xKhTmLbOuNdArY";
    
    bodyStr = [bodyStr stringByAppendingString:@"\r\n"];
    
    bodyStr = [bodyStr stringByAppendingString:@"Content-Disposition: form-data; name=\"cityid\"\r\n\r\n"];
    bodyStr = [bodyStr stringByAppendingString:[parmaters objectForKey:@"cityid"]];
    bodyStr = [bodyStr stringByAppendingString:@"\r\n"];
    
    bodyStr = [bodyStr stringByAppendingString:@"--0xKhTmLbOuNdArY\r\n"];
    bodyStr = [bodyStr stringByAppendingString:@"Content-Disposition: form-data; name=\"hid\"\r\n\r\n"];
    bodyStr = [bodyStr stringByAppendingString:[parmaters objectForKey:@"hid"]];
    bodyStr = [bodyStr stringByAppendingString:@"\r\n"];
    bodyStr = [bodyStr stringByAppendingString:@"--0xKhTmLbOuNdArY\r\n"];
    //    bodyStr = [bodyStr stringByAppendingString:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"];
    bodyStr = [bodyStr stringByAppendingString:@"--0xKhTmLbOuNdArY—-"];
    
    
    //    --0xKhTmLbOuNdArY
    //    Content-Disposition: form-data; name="cityid"
    //
    //    dl
    //    --0xKhTmLbOuNdArY
    //    Content-Disposition: form-data; name="hid"
    //
    //    160
    //    --0xKhTmLbOuNdArY--
    
    
    NSLog(@"%@", bodyStr);
    
    
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:str];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    [request setHTTPMethod:@"POST"];
    [request setValue:@"multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY" forHTTPHeaderField:@"Content-Type"];
    
    NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    
    
    [request setHTTPBody:data];
    [NSURLConnection connectionWithRequest:request delegate:self];
    
    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.receiveData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_receiveData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
    NSString *value = [[NSString alloc] initWithData:self.receiveData encoding:enc];
    
    NSMutableArray *arr = (NSMutableArray *)[value componentsSeparatedByString:@"]"];
    
    NSString *newstr = [NSString string];
    for (NSString *nsstring in arr) {
        if ([nsstring isEqualToString:[arr objectAtIndex:0]] ) {
            NSString *str1 = [NSString stringWithFormat:@"%@],", nsstring];
            newstr = [newstr stringByAppendingString:str1];
        }
        else if ([nsstring isEqualToString:[arr lastObject]]) {
            newstr = [newstr stringByAppendingString:nsstring];
        } else {
            NSString *str  = [NSString stringWithFormat:@"%@]", nsstring];
            newstr = [newstr stringByAppendingString:str];
        }
        
        
    }
    NSString *str3 = @",\"zfmap\"";
    NSMutableArray *twoArr = [NSMutableArray array];
    twoArr = [NSMutableArray arrayWithArray:[newstr componentsSeparatedByString:@"\"zfmap\""]];
    NSString *newTwo = [twoArr objectAtIndex:0];
    newTwo = [newTwo stringByAppendingString:str3];
    if (twoArr.count > 1) {
         newTwo = [newTwo stringByAppendingString:[twoArr objectAtIndex:1]];
        
    }
    
    NSData *data = [newTwo dataUsingEncoding:NSUTF8StringEncoding];
    [self.delegate connectFinished:data];
    NSLog(@"data = ===== %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    [Tools writeWithName:self.requestUrl data:data directory:NSLibraryDirectory];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error = %@", error);
}
//
//

@end
